﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using System.IO;
using log4net;
using log4net.Config;

namespace ThreadTest
{
    public partial class MessageMonitor : Form
    {
        ILog logger = LogManager.GetLogger(typeof(MessageMonitor));

        Client client = new Client("noc-rates-dev1", 7);
        delegate void StringParameterDelegate(string value);

        public MessageMonitor()
        {
            InitializeComponent();
        }

        private void MissionControl_Click(object sender, EventArgs e)
        {            
            Thread task = new Thread(MessageSenderReceiver);
            task.IsBackground = true;
            task.Start();            
        }

        private void MessageSenderReceiver()
        {
            String messageForQueue = messageToGo.Text;
            client.pushMessage(messageForQueue);
            String output = client.pollMessage();
            UpdateStatus(output);
        }

        void UpdateStatus(string value)
        {
            if (InvokeRequired)
            {
                // We're not in the UI thread, so we need to call BeginInvoke
                BeginInvoke(new StringParameterDelegate(UpdateStatus), new object[] { value });
                return;
            }
            // Must be on the UI thread if we've got this far
            messageGot.Text = value;
        }

        static void Main()
        {
            XmlConfigurator.Configure(new FileInfo("log4netconfig.xml"));
            Application.Run(new MessageMonitor());
        }        
    }
}
